package cool.taomu.software.fig.mqtt.broker.impl

import cool.taomu.software.fig.mqtt.broker.MQTTCache
import cool.taomu.software.fig.mqtt.broker.entity.MessageEntity
import cool.taomu.software.fig.mqtt.broker.inter.IPublishObserver.Type
import cool.taomu.software.fig.rpc.annotation.Rpc
import cool.taomu.software.fig.rpc.inter.IRpc
import cool.taomu.software.fig.utils.RuntimeStateUtils
import cool.taomu.software.fig.utils.RuntimeStateUtils.RuntimeState

@Rpc(RpcPublish)
interface IRpcPublish extends IRpc {
    def void publish(MessageEntity message);

    def void refreshRetain();

    def RuntimeState getRuntimeState();
}

class RpcPublish implements IRpcPublish {
    MQTTCache cache = MQTTCache.instance;

    override publish(MessageEntity message) {
        cache.store("RPCPublish", message);
        // 推送用户订阅
        PublishObservable.instance.start("RPCPublish", Type.MESSAGE);
    }

    override refreshRetain() {
        cache.refreshRetain();
    }

    override getRuntimeState() {
        var rsu = new RuntimeStateUtils();
        return rsu.runtimeState;
    }

}
